<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    //
    // 浏览订单
    public function index(Request $request)
    {
    	$num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('order') -> where('tel','like','%'.$keywords.'%') -> paginate($num) ;
        foreach ($data as $k => $v)
        {
        	// 获取用户名
        	$data[$k] -> uid = DB::table('users') -> where('id',$data[$k] -> uid) -> first() -> name;
        	// 将数字的订单状态转换为文字
        	if($data[$k] -> status == 0)
        	{
        		$data[$k] -> status = '未发货';
        	}elseif($data[$k] -> status == 1)
        	{
        		$data[$k] -> status = '已发货,未签收';
        	}else
        	{
        		$data[$k] -> status = '已签收';
        	}
        }
       
        return view('admin.order.index',['data' => $data ,'request' => $request -> all(), 'title' => '订单列表']);
    }

    // 订单详情表
    public function detail($id)
    { 
    	// 获取订单收货人信息
    	$orderdata = DB::table('order') -> where('id', $id) ->first();
    	// 获取订单商品信息
    	$detaildata = DB::table('detail') -> where('oid', $id) ->get();
    	// dd($detaildata);
    	// 获取用户名
    	$username = DB::table('users') 
    				-> where('id', $orderdata -> uid) 
    				-> first()
    				-> name;
    	// dd($username);
    	// dd($detaildata);
    	foreach ($detaildata as $k => $v)
    	{
    		// 获取商品信息
    		$detaildata[$k] -> gid = DB::table('goods') -> where('gid',$detaildata[$k] ->gid) -> first();
    		$pid = $detaildata[$k] -> gid -> pid;
    		// 获取商品分类名
    		$detaildata[$k] -> gid -> pid = DB::table('category') -> where('id', $pid) ->first() -> name;
    	}
    	// dd($detaildata[0] ->gid);
    	return view('admin.order.detail',['orderdata' => $orderdata, 'detaildata' => $detaildata, 'username' =>$username, 'title' => '订单详情']);
    }

    // 修改订单收货人信息
    public function update(Request $request)
    {
        // dd($request);
        $id = $request -> id;
        // $pic = $request -> file('pic');
        $data = $request -> except('_token','id','name');
        // dd($data);
        // dd($request);                                  
  
        $res = DB::table('order') -> where('id',$id) -> update($data);

        if($res)
        {
        	return redirect('/admin/order/index') -> with(['info' => '修改成功']);
        }else
        {
        	return back() -> with(['info' => '修改失败']);
        }
    }

    // 发货
    public function send($id)
    {	
    	$data['status'] = 1;
    	$res = DB::table('order') -> where('id',$id) -> update($data);
    	if($res)
        {
        	return redirect('/admin/order/index') -> with(['info' => '发货成功']);
        }else
        {
        	return back() -> with(['info' => '发货失败']);
        }
    }
    
    // 删除订单
    public function delete($id)
    {
        // dd($id);
        // 开启事务
        DB::beginTransaction();
        $res = DB::table('order') -> where('id',$id) -> delete();
      	if($res)
        {	
        	$res2 = DB::table('detail') -> where('oid',$id) -> delete();
        	if($res2)
        	{
        		DB::commit();
        		return redirect('/admin/order/index') -> with(['info' => '删除成功']);
        	}else
        	{
        		DB::rollback();
        		return back() -> with(['info' => '删除失败']);
        	}
        	
        }else
        {	
        	DB::rollback();
        	return back() -> with(['info' => '删除失败']);
        }
    }

}
